#include<stdio.h>
#include<stdlib.h>
struct node
{
  	int data;
   	struct node *next;
}*p;
append(int value)
{	struct node *new,*np;
	new= (struct node *)malloc(sizeof(struct node));
	new->data=value;
	new->next=NULL;
	np=p;
	if(p==NULL)
	{	p=new;
	}
	else
	{	while(np->next!=NULL)
		np=np->next;
		np->next=new;
		np=new;
	}
}
displayatpos(int pos)
{	struct node *np;
   	int c=0;
   	np=p;
    	while(np!=NULL)
   	{	if(c==pos)
		{	printf("value at %d node is %d\n\n",pos,np->data);
			break;
		}
     		c++;
		np=np->next;     		
	}
}
int counts()
{
   	struct node *np;
   	int nonodes=0;
   	np=p;
    	while(np!=NULL)
   	{
     		np=np->next;
     		nonodes=nonodes+1;
   	}
  	return(nonodes);
}
slicing(int pos1,int pos2)
{	struct node *np;
	int i,c=0,nonodes;
	np=p;
	nonodes=counts();
	if(pos2>nonodes)
	  pos2=nonodes;
	if((pos1<0)||(pos1>pos2))
	  printf("\nslicing is not possible:\n\n");
	while(np!=NULL)
	{	if(c==pos1)
		{	for(i=c;i<pos2;i++)
			{	printf("->%d",np->data);
				np=np->next;
			}
			break;
		}
		c++;
		np=np->next;
	}
}
count(int num)
{	struct node *np;
	int c=0;
	np=p;
	while(np!=NULL)
	{	if(np->data==num)
		{	c++;
		}
		np=np->next;
	}
	printf("total number of %d is %d\n\n",num,c);
}
findpos(int num)
{	struct node *np;
	int c=0;
	np=p;
	while(np!=NULL)
	{	
		if(np->data==num)
		{	printf("element is found at position %d\n\n",c);
			break;
		}
		c++;
		np=np->next;
	}
	if(np==NULL)
	  printf("element not found\n\n");
}
reverse(struct node *first)
{
   	struct node *temp, *rev,*l,*prevalue;
   	temp=first;
   	rev=NULL;
  	while(temp!=NULL)
 	{
   		prevalue=rev;
   		rev=temp;
  		temp=temp->next;
  		rev->next=prevalue;
 	}
   	p=rev;
}
pop()
{	int pos,i,x;
	printf("enter the position");
	scanf("%d",&pos);	
	struct node *np,*mp,*ptr;
	np=p;
	x=counts();
	if(np==NULL)
	{	printf("UNDERFLOW!!!!");
	}
	else if(pos==0)
	{	ptr=np;
		np=np->next;
		free(ptr);
		p=np;
		
	}	
	else
	{	for(i=0;i<x;i++)
		{	if(pos==i)
			{	mp->next=np->next;
				free(np);
				return;
			}
			else
			{	mp=np;
				np=np->next;
			}
		}	
	}
			
}
void extend(int sizes)
{
	int i=0,j,v;
	struct node *t,*tmp,*q;
	printf("\nENTER element %d :",i+1);
	scanf("%d",&v);
	t=(struct node *)malloc(sizeof(struct node));
	t->data=v;
	t->next=NULL;	
	for(i=1;i<sizes;i++)
	{
		printf("ENTER element %d :",i+1);
		scanf("%d",&v);
		tmp= (struct node *)malloc(sizeof(struct node));
		tmp->data=v;
		tmp->next=NULL;
		q=t;
		while(q->next!=NULL)
			q=q->next;
		q->next=tmp;
	}
	q=t;
	while(q!=NULL)
	{
	printf("%d",q->data);
	q=q->next;
	}
	q=p;
	while(q->next!=NULL)
		q=q->next;
	q->next=t;
}
rem(int num)
{
     	struct node *np, *mp;
	np=p;
      	while(np!=NULL)
    	{
       		if(np->data==num)
       		{
           		if(np==p)
           		{
              			p=np->next;
              			free(np);
              			return;
           		}
           		else
         		{
           			mp->next=np->next;
           			free(np);
           			return;
         		}
      		}
		else
        	{
           		mp=np;
          		np=np->next;
        	}

	}
    	printf("ELEMENT %d NOT FOUND \n \n", num);
}
insertatgiv(int value,int pos)
{	pos=pos-1;
	int i,x=0;
	struct node *temp,*orig,*curent,*nt;
	orig=p;
	nt=p;
    	while(nt!=NULL)
   	{
     		nt=nt->next;
     		x=x+1;
   	}
	if(pos>x||pos<0)
   	{
         	printf("insertion is not possible :\n\n");
            	return;
  	}
	if(pos==0)
	{	temp=(struct node *)malloc(sizeof(struct node));
        	temp->data=value;
		if (orig== NULL)
       		{
          		orig=temp;
          		orig->next=NULL;
       		}
		else
		{	temp->next=orig;
			orig=temp;
		}
		p=orig;
		return;
	}
	else if(pos<x);
	{	for(i=0;i<pos;i++)
		{	curent=orig;
			orig=orig->next;
		}	
		temp=(struct node *)malloc(sizeof(struct node));
		temp->data=value;
		curent->next=temp;
		curent=temp;
		curent->next=orig;
		return;
	}
}
display(struct node *np)
{
        np=p;
      	if(np==NULL)
     	{
       		printf("NO ELEMENT IN THE LIST :\n \n");
       		return;
     	}
	printf("[");
       	while(np!=NULL)
    	{
      		printf("%d  ",np->data);
      		np=np->next;
    	}
     	printf("]\n\n");
}
void del_all_nodes()
{
	struct node *tmp;
	tmp=p;
	while(tmp!=NULL)
	{
		p=p->next;
		free(tmp);
		tmp=p;
	}
	exit(EXIT_SUCCESS);
}
void sort()
{
  struct node *t,*tmp,*a;
  int i, j, k,nonodes;
  int min, temp;
  nonodes=counts();
  a=p;
  for (i = 0; i < nonodes; i++)
  {
    tmp=a->next;
    t=a;
//min = i;
  for (j = i+1; j < nonodes; j++)
  {
     if (tmp->data < t->data)
	t=tmp;
     tmp=tmp->next;
  }
  temp = a->data;
  a->data=t->data;
  t->data = temp;
  a=a->next;
  }
}
main()
{
       	int i,num,j;
   	p=NULL;
	char want;
	want='y';
	while(want=='y')
	{	printf("\nPLEASE ENTER THE NUMBER :-\n");
        	scanf("%d",&num);
        	append(num);
		printf("want to enter more press y else press another key:\n");
		scanf("%s",&want);
	}
	int choice=1;
   	while(choice==1)
 	{
    		printf("\n1.Append:");
    		printf("\n2.Display element at given position:");
    		printf("\n3.Slicing:");
    		printf("\n4.Count:");
    		printf("\n5.Indexing");
    		printf("\n6.Reverse:");
    		printf("\n7.Sorting:");
    		printf("\n8.Pop:");
    		printf("\n9.Remove:");
		printf("\n10.insert element at given position:");
    		printf("\n11.Extend:");
		printf("\n0.EXIT:");
		printf("\n\nENTER your CHOICE :");
		scanf("%d",&i);
		switch(i)
    		{
         		case 1:
      			{
        			int newel;
        			printf("\nPLEASE ENTER THE NUMBER :\n");
        			scanf("%d",&newel);
        			append(newel);
				display(p);
        			break;
      			}
          		case 2:
       			{
         			int pos;
         			printf("\nPLEASE ENTER THE POSITION :\n");
         			scanf("%d",&pos);
         			displayatpos(pos);
        			break;
      			}
			case 3:
     			{
      				int pos1,pos2;
      				printf("\nPLEASE ENTER THE STARTING POSITION :-\n");
      				scanf("%d",&pos1);
      				printf("\nPLEASE ENTER THE ANOTHER POSITION :-\n");
      				scanf("%d",&pos2);
      				slicing(pos1,pos2);
      				break;
    			}
			case 4:
      			{
 				int num1;
				printf("ENTER THE NUMBER");
				scanf("%d",&num1);
				count(num1);
         			break;
      			}
			case 5:
   			{
      				int num2;
				printf("ENTER THE NUMBER");
				scanf("%d",&num2);
				findpos(num2);
      				break;
   			}
			case 6:
    			{	reverse(p);
        			display(p);
        			break;
            			
    			}
			case 7:
			{
				sort();
				display(p);
				break;
			}
			case 8:
			{	pop();
				display(p);
				break;
			}
			case 9:
    			{
            			int num;
      				printf("\nPLEASE ENTER A NUMBER FROM THE LIST :\n");
      				scanf("%d",&num);
      				rem(num);
				display(p);
     				break;
    			}
			case 10:
			{
				int newnum, posat,k;
      				printf("\nPLEASE ENTER THE NUMBER :\n");
      				scanf("%d",&newnum);
      				printf("\nPLEASE ENTER THE LOCATION NUMBER :\n");
      				scanf("%d",&posat);
      				insertatgiv(newnum,posat+1);
				display(p);
      				break;	
   			}
			case 11:
			{	int sizes;
				printf("enter no. of values you want to enter :");
				scanf("%d",&sizes);
				extend(sizes);
				break;
			}
			case 0:
			  del_all_nodes();
    		}
		printf("\nWant to do more operations press 1 else other Key to exit:-");
		scanf("%d",&choice);
 	}
}

















